package com.iotechn.unimall.app.api.notice;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.UserNoticeDO;
import com.iotechn.unimall.data.dto.UserNoticeCountDTO;
import com.iotechn.unimall.data.mapper.UserNoticeMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ide-eval-resetter-2.1.6
 * Created by rize on 2019/7/4.
 */
@Service
public class UserNoticeServiceImpl implements UserNoticeService {

    private static final Logger logger = LoggerFactory.getLogger(UserNoticeServiceImpl.class);
    @Autowired
    private UserNoticeMapper userNoticeMapper;

    @Override
    public Page<UserNoticeDO> getUserNoticeByUserId(Integer pageNo, Integer pageSize, Integer isread, Long userId) throws ServiceException {
        Wrapper<UserNoticeDO> wrapper = new EntityWrapper<UserNoticeDO>();
        if (isread != null) {
            wrapper.eq("isread", isread);
        }
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }

        List<UserNoticeDO> advertisementDOList = userNoticeMapper.selectPage(new RowBounds(pageSize * (pageNo - 1), pageSize), wrapper);
        Integer count = userNoticeMapper.selectCount(wrapper);

        Page<UserNoticeDO> page = new Page<>(advertisementDOList, pageNo, pageSize, count);
        return page;
    }

    @Override
    public Page<UserNoticeDO> getUserNoticeByShopId(Integer pageNo, Integer pageSize, Integer isread, Integer shopId, Long userId) throws ServiceException {
        Wrapper<UserNoticeDO> wrapper = new EntityWrapper<UserNoticeDO>();
        if (isread != null) {
            wrapper.eq("isread", isread);
        }
        if (userId != null) {
            wrapper.eq("shop_id", shopId);
        }
        List<UserNoticeDO> advertisementDOList = userNoticeMapper.selectPage(new RowBounds(pageSize * (pageNo - 1), pageSize), wrapper);
        Integer count = userNoticeMapper.selectCount(wrapper);
        Page<UserNoticeDO> page = new Page<>(advertisementDOList, pageNo, pageSize, count);
        return page;
    }

    @Override
    public UserNoticeCountDTO selectUnreadCountByUserId(Long userId) {
        return userNoticeMapper.selectUnreadCountByUserId(userId);
    }

    @Override
    public UserNoticeCountDTO selectUnreadCountByShopId(Long shopId, Long userId) {
        return userNoticeMapper.selectUnreadCountByShopId(shopId);

    }

    @Override
    public Boolean updateIsRead(Long id, Long userId) {
        return userNoticeMapper.updateIsRead(id);

    }

    @Override
    public Boolean updateIsReadAll(Long userId) {
        return userNoticeMapper.updateIsReadAll(userId);

    }

    @Override
    public Boolean updateIsReadAllByShopId(Long shopId, Long userId) {
        return userNoticeMapper.updateIsReadAllByShopId(shopId);

    }
}
